package com.elvin.platform.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.elvin.platform.beans.Organize;
import com.elvin.platform.beans.PageVo;
import com.elvin.platform.beans.UserOrganization;
import com.elvin.platform.service.UserOrganizationService;
import com.elvin.platform.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zhenchl
 * @since 2022-05-27
 */
@Controller
@RestController
@RequestMapping("/userOrganization")
@Log4j2
@Api("用户组织管理")
public class UserOrganizationController {

  @Resource
  private UserOrganizationService service;

  @ApiOperation(value = "删除用户组织信息 ")
  @PostMapping("/delete")
  public Result<Object> delete(@ApiParam(name = "id", value = "用户组织信息id", required = true) @RequestParam("id") Long id) {
    Result<Object> result = new Result<>();
    try {
      service.deleteById(id);
      result.success("删除成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiOperation(value = "根据ID获取用户组织信息")
  @PostMapping("/getById")
  public Result<Object> getById(@ApiParam(name = "id", value = "用户组织信息id", required = true) @RequestParam("id") Long id) {
    Result<Object> result = new Result<>();
    try {
      UserOrganization entity = new UserOrganization();
      entity.setId(id);
      entity = service.selectById(entity);
      result.success(entity);
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiOperation(value = "根据用户ID获取组织及子组织机构信息")
  @PostMapping("/getOrganizationsByUserId")
  public Result<Object> getOrganizationsByUserId(
      @ApiParam(name = "userId", value = "用户id", required = true) @RequestParam("userId") Long userId
  ) {
    Result<Object> result = new Result<>();
    try {
      Map<String, Object> map = new HashMap<>();
      map.put("userId", userId);
      List<Organize> list = service.getOrganizationsByUserId(map);
      result.success(list);
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiOperation(value = "新增用户组织信息")
  @PostMapping("/insert")
  public Result<Object> insert(@RequestBody UserOrganization entity) {
    Result<Object> result = new Result<>();
    try {
      service.insert(entity);
      result.success(entity, "新增成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiIgnore
  @ApiOperation(value = "获取所有用户组织信息")
  @PostMapping("/queryAll")
  public Result<Object> queryAll() {
    Result<Object> result = new Result<>();
    try {
      List<UserOrganization> selectList = service.selectList(null);
      result.success(selectList);
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiIgnore
  @ApiOperation(value = "分页获取所有数据")
  @PostMapping("/selectPageBefore")
  public Result<Object> selectForPage(
      @ApiParam("当前页码") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
      @ApiParam("当前页显示条数") @RequestParam(value = "pageSize", required = false) Integer pageSize
  ) {
    Result<Object> result = new Result<>();
    try {
      if (pageNumber != null && pageSize != null) {
        Page<UserOrganization> page = new Page<>(pageNumber, pageSize);
        service.selectPage(page, null);
        List<UserOrganization> selectList = page.getRecords();
        result.success(selectList);
      } else {
        List<UserOrganization> selectList = service.selectList(null);
        result.success(selectList);
      }

    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiOperation(value = "分页获取所有数据")
  @PostMapping("/selectPage")
  public Object selectForPage(
      @ApiParam("当前页码") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
      @ApiParam("当前页显示条数") @RequestParam(value = "pageSize", required = false) Integer pageSize,
      @ApiParam("用户id") @RequestParam(value = "user_id", required = false) Long userId,
      @ApiParam("组织id") @RequestParam(value = "organize_id", required = false) Long organizeId
  ) {
    Map<String, Object> map = new HashMap<String, Object>();
    try {
      UserOrganization entity = new UserOrganization();
      entity.setOrganizeId(organizeId);
      entity.setUserId(userId);
      int total = service.selectPageCount(entity);
      if (total > 0) {
        PageVo pageVo = new PageVo();
        if (pageNumber != null && pageSize != null) {
          pageVo.setPage(pageNumber);
          pageVo.setPageSize(pageSize);
        } else {
          pageVo.setPage(1);
          pageVo.setPageSize(100000000);
        }
        entity.setPageVo(pageVo);
        List<UserOrganization> list = service.selectForPage(entity);
        map.put("data", list);
      } else {
        map.put("data", null);
      }
      map.put("status", 0);
      map.put("total", total);
      map.put("message", "查询成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
    }
    return map;
  }

  @ApiOperation(value = "修改用户组织信息")
  @PostMapping("/update")
  public Result<Object> update(@RequestBody UserOrganization entity) {
    Result<Object> result = new Result<>();
    try {
      service.updateById(entity);
      result.success("修改成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }


  @ApiOperation(value = "新增用户组织信息")
  @PostMapping("/addAndCheck")
  public Result<Object> addAndCheck(@ApiParam("用户id") @RequestParam(value = "user_id", required = true) Long userId,
                                    @ApiParam("组织id") @RequestParam(value = "organize_id", required = true) Long organizeId,
                                    @ApiParam("组织名称") @RequestParam(value = "organize_name", required = true) String organizeName) {
    Result<Object> result = new Result<>();
    try {
      Map<String,Object> map = new HashMap<>();
      UserOrganization entity = new UserOrganization();
      entity.setOrganizeId(organizeId);
      entity.setOrganizeName(organizeName);
      entity.setUserId(userId);
      result = service.addAndCheck(entity);
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }


}
